﻿#include "FITKAbstractSEASubSysWithNodes.h"
#include "FITKAbstractSEAFace.h"
#include "FITKAcousticsDiscModel.h"
#include "FITKAcousticsSEAModel.h"
#include "FITK_Kernel/FITKCore/FITKDataRepo.h"

namespace Acoustics
{
    FITKAbstractSEASubSysWithNodes::~FITKAbstractSEASubSysWithNodes()
    {
        //解除face关联
        FITKAbstractSEAFace* f = this->getFaceObj();
        if (!f) return;
        f->removeConnector(this);
    }

    void FITKAbstractSEASubSysWithNodes::setNodes(const QList<SubSysNode>& nodes)
    {
        FITKAbstractSEAFace* face = this->getFaceObj();
        //while循环避免嵌套，代替if判断
        while(face == nullptr)
        {
            face = FITKSEAFaceManager::CreateSEAFace(this->getSEASubSysType());
            if (!face) break;;
            this->setFaceID(face->getDataObjectID());
            //获取管理器
            FITKAcousticsDiscModel* disc = FITKAcousticsDiscModel::GetDiscModelFromFrame();
            if (disc == nullptr) break;
            FITKAcousticsSEAModel* SEA = disc->getSEAModel();
            if (SEA == nullptr) break;
            FITKSEAFaceManager* mgr = SEA->getSEAFaceManager();
            if (mgr == nullptr) break;
            //追加到管理器
            mgr->appendDataObj(face);
            break;
        }
        if (face) face->setNodes(nodes);
    }

    QList<FITKAbstractSEASubSysWithNodes::SubSysNode> FITKAbstractSEASubSysWithNodes::getNodes()
    {
        //从基础对象获取
        FITKAbstractSEAFace* face = this->getFaceObj();
        if (face) return face->getNodes();
        return QList<FITKAbstractSEASubSysWithNodes::SubSysNode>();
    }

    QList<int> FITKAbstractSEASubSysWithNodes::getNodeIDs() const
    {
        //从基础对象获取
        FITKAbstractSEAFace* face = this->getFaceObj();
        if (face) return face->getNodeIDs();
        return QList<int>();
    }


    void FITKAbstractSEASubSysWithNodes::setFaceID(const int & pid)
    {
        
        FITKAbstractSEAFace* face = FITKDATAREPO->getTDataByID<Acoustics::FITKAbstractSEAFace>(pid);;
        if (!face) return;
        face->addConnector(this);
        _faceID = pid;
    }


    int FITKAbstractSEASubSysWithNodes::getFaceID() const
    {
        return _faceID;
    }

    bool FITKAbstractSEASubSysWithNodes::updateGeoProp()
    {
        bool OK = true;
        FITKAbstractSEAFace* face = FITKDATAREPO->getTDataByID<Acoustics::FITKAbstractSEAFace>(_faceID);
        if (face)
        {
            OK &= face->updateModelProp();
        }
        OK &= this->FITKAbstractSEASubSys::updateGeoProp();
        return OK;
    }

    Acoustics::FITKAbstractSEAFace* FITKAbstractSEASubSysWithNodes::getFaceObj() const
    {
        return FITKDATAREPO->getTDataByID<Acoustics::FITKAbstractSEAFace>(_faceID);
    }

    bool FITKAbstractSEASubSysWithNodes::isContainsNode(const int id) const
    {
        QList<int> nodes = this->getNodeIDs();
        return nodes.contains(id);
    }

}